package com.smart.seckilldemo.service.impl;

import com.smart.seckilldemo.commons.Parameters;
import com.smart.seckilldemo.commons.dto.OrderDto;
import com.smart.seckilldemo.commons.exception.ServiceException;
import com.smart.seckilldemo.commons.exception.StatusCode;
import com.smart.seckilldemo.commons.vo.OrderVo;
import com.smart.seckilldemo.mapper.OrderMapper;
import com.smart.seckilldemo.service.OrderService;
import com.smart.seckilldemo.utils.RedisJudgeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    OrderMapper orderMapper;
    @Resource
    RedisJudgeUtils redisJudgeUtils;

    /**
     * 添加订单
     * @param order
     * @return
     */
    @Override
    public Integer interOrder(OrderDto order) {
       return 0;
    }

    //获取用户的全部订单信息
    @Override
    public List<OrderVo> findOrderByUserId(Long userId) {
        return orderMapper.findOrderDetailByUserId(userId);
    }

    /**
     * 根据订单号查询订单详情
     *
     * @param orderId
     * @return
     */
    @Override
    public OrderVo detail(Long orderId) {
        String redisKey= Parameters.SeckillOrderDateCache+":"+orderId;
        OrderVo red = (OrderVo) redisJudgeUtils.getRed(redisKey);
        if(red!=null){
            return red;
        }else {
            OrderVo orderDetail = orderMapper.findOrderDetail(orderId);
            if(orderDetail==null){
                throw new ServiceException(StatusCode.ORDER_NUM_ERROR);
            }
            if(orderDetail!=null){
                redisJudgeUtils.setRed(redisKey,orderDetail);
            }
            return orderDetail;
        }
    }
}
